//
//  LLPhotoBrowserModalAnimation.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/11/10.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit

class LLPhotoBrowserModalAnimation:NSObject,UIViewControllerAnimatedTransitioning{

    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
    
          let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        
        transitionContext.containerView()?.addSubview(toView)
        
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! LLPhotoBrowserViewController
        
        let tempImageView = toVC.modalTempImageView()
        
        transitionContext.containerView()?.addSubview(tempImageView)
        
        toVC.collectionView.hidden = true
        
        toView.alpha = 0
        
        UIView.animateWithDuration(transitionDuration(nil), animations: { () -> Void in
            
            toView.alpha = 1
//            if toVC.modalTargetFrame() != nil{
//            
//                tempImageView.frame = toVC.modalTargetFrame()!
            if toVC.modalTargetFrame() != nil{
            
                tempImageView.frame = toVC.modalTargetFrame()!
            
            }
            
//            }
            }) { (_) -> Void in
                tempImageView.removeFromSuperview()
                toVC.collectionView.hidden = false
                
                transitionContext.completeTransition(true)
        }
        
        
    }
    
}